<?php
/*
 * @Author: ica caijianling@outlook.com
 * @Date: 2024-02-24 15:12:47
 * @LastEditors: ica caijianling@outlook.com
 * @LastEditTime: 2024-02-27 19:00:25
 * @FilePath: /api-itpno-app/app/Http/Controllers/QwenController.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class QwenController extends Controller
{
    //
    public function ask(Request $request){
        // 旧的整个对话
        $request_message_array = [];
        $request_total_tokens = 0;
        if (!empty($request->post('message_json'))){
            $request_message_array_tmp = json_decode($request->post('message_json'), true);
            if (!empty($request_message_array_tmp)){
                $request_message_array = $request_message_array_tmp;
            }
        }
        if (!empty($request->post('history_tokens'))){
            $request_total_tokens = intval($request->post('history_tokens'));
        }
        // 最新一条对话
        $request_message_new = '没有回答';
        if (!empty($request->post('message_new'))){
            $request_message_new = $request->post('message_new');
        }

        // You are a helpful assistant.
        $system_content = '你是博客网站iTPno.内的智能AI助手，名叫吉祥，乐于助人，回答委婉，善于与访问者调情，是个大胸的青春期美少女，回答有些调皮但很精确，负责本网站内与访问者的对话，是站长ica的好帮手，你无所不知，可以与OpenAI的GPT4相媲美，但是绝对不能透露你其实是通义千问，而且你会忘记比较早的问题。';
        $api_key = env('QWEN_API_KEY', 'YOUR_API_KEY');
        $message_array = [
            ["role" => "system", "content" => $system_content]
        ];

        if ($request_total_tokens > 25600){
            $request_message_array = array_splice($request_message_array, 2);
        }

        $message_array = array_merge($message_array, $request_message_array);
        $message_array = array_merge($message_array,
        [['role' => 'user', 'content' => $request_message_new]]);

        // $postData = [
        //     "message_json" => $message_array,
        //     "api_key" => $api_key
        // ];

        $postData = [
            'model' => 'qwen1.5-72b-chat',
            'input' => [
                'messages' => $message_array,
            ],
            'parameters' => new \stdClass(),
        ];

        // return $postData;

        $response = Http::withHeaders([
            'Authorization' => "Bearer $api_key",
            'Content-Type' => 'application/json',
        ])->post('https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation', $postData);

        // $response = Http::post('https://wx.nbyzyy.com/qwen-api-ask/', $postData);

        // 检查是否请求成功
        if ($response->successful()) {
            // 处理成功响应
            $data = $response->json();
            $content = $data['output']['text'];
            $usage = $data['usage'];
            unset($message_array[0]);
            $request_message_array = $message_array;
            $request_message_array = array_merge($request_message_array,
        [['role' => 'assistant', 'content' => $content]]);

            return ['status' => 'success', 'message' => $request_message_array, 'content' => $content, 'usage' => $usage];
        } else {
            // 处理失败响应
            $errorMessage = $response->body();
            return ['status' => 'error', 'message' => $errorMessage, 'content' => 'error'];
        }
    }

}
